Source Code Viewer - Example 5 - ViewGameListAction

package name.matthewgreet.example5.action;

import java.util.stream.Collectors;

import javax.servlet.http.HttpSession;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import com.opensymphony.xwork2.ActionSupport;

import name.matthewgreet.example5.action.FindGameListByDesignerNameAction.GameIdFinderByDesigner;
import name.matthewgreet.example5.action.FindGameListByFamilyAction.GameListFinderByFamily;
import name.matthewgreet.example5.action.FindGameListByNameContainsAction.GameListFinderByNameContains;
import name.matthewgreet.example5.action.FindGameListByRankAction.GameListSizeFinderByRank;
import name.matthewgreet.example5.common.AbstractViewActionSupport;
import name.matthewgreet.example5.common.ListCache;
import name.matthewgreet.example5.common.NA;
import name.matthewgreet.example5.dto.GameDTO;
import name.matthewgreet.example5.exception.ConfigurationException;
import name.matthewgreet.example5.form.DesignerNameForm;
import name.matthewgreet.example5.form.GameFamilyForm;
import name.matthewgreet.example5.form.GameNameContainsForm;
import name.matthewgreet.example5.form.GameRankForm;
import name.matthewgreet.example5.model.DesignerTable;
import name.matthewgreet.example5.model.GameTable;
import name.matthewgreet.example5.model.Model;
import name.matthewgreet.example5.util.GameCompositeCache;
import name.matthewgreet.example5.util.GameCompositeCache.GameItemSorter;
import name.matthewgreet.example5.util.GameCompositeCache.GameItemSorterCategories;
import name.matthewgreet.example5.util.GameCompositeCache.GameItemSorterDesigners;
import name.matthewgreet.example5.util.GameCompositeCache.GameItemSorterName;
import name.matthewgreet.example5.util.GameCompositeCache.GameItemSorterOwners;
import name.matthewgreet.example5.util.GameCompositeCache.GameItemSorterRank;
import name.matthewgreet.example5.util.GameCompositeCache.GameItemSorterRating;
import name.matthewgreet.example5.util.GameCompositeCache.GameItemSorterWeight;
import name.matthewgreet.example5.util.GameCompositeCache.GameItemSorterYear;
import name.matthewgreet.example5.util.GameCompositeCache.GameSortColumn;
import name.matthewgreet.example5.view.GameListDisplay;

/**
 * Displays search results of board games.  
 */
@InterceptorRef("ViewStack")
@Results({
	@Result(name=ActionSupport.SUCCESS, location="/pages/GameList.jsp")
})
public class ViewGameListAction extends AbstractViewActionSupport {
	public enum GameCategory {ALL, STRATEGY, ABSTRACT, FAMILY, THEMATIC, CUSTOMIZABLE, WAR, PARTY, CHILDREN}
	
	private static final long serialVersionUID = -7979376337555804889L;
	
	public static final int PAGE_SIZE 	= 6;
	
    private Logger LOG = LogManager.getLogger(ViewGameListAction.class);
    
	private String refresh;
	private Integer pageNo;
	private String sort;
	private Boolean ascending;
	
	private GameNameContainsForm gameNameContainsForm;
	private GameRankForm gameRankForm;
	private DesignerNameForm designerNameForm;
	private GameFamilyForm gameFamilyForm;
	
	private GameListDisplay listDisplay;
	
	private String designersAutocompleteText;
	private String familiesAutocompleteText;
    private String sortOption;
    private boolean sortAscending;

	
    private GameItemSorter getItemSorter(GameCategory preferredCategory) {
    	GameSortColumn gameSortColumn;
        boolean workingAscending;
        
        if (sort.length() > 0) {
	        try {
	        	gameSortColumn = GameSortColumn.valueOf(sort);
	            if (ascending == null) {
	                workingAscending = !gameSortColumn.getDefaultDescending();
	            } else {
	                workingAscending = ascending;
	            }
	            switch (gameSortColumn) {
				case CATEGORIES:return new GameItemSorterCategories(workingAscending);
				case DESIGNERS:	return new GameItemSorterDesigners(workingAscending);
	            case NAME:      return new GameItemSorterName(workingAscending);
				case OWNERS:	return new GameItemSorterOwners(workingAscending);
				case RANK:		return new GameItemSorterRank(preferredCategory, workingAscending);
				case RATING:	return new GameItemSorterRating(workingAscending);
				case WEIGHT:	return new GameItemSorterWeight(workingAscending);
				case YEAR:		return new GameItemSorterYear(workingAscending);
	            }
	        }
	        catch (IllegalArgumentException e) {
	        	LOG.error("Sort option parameter is unrecognisable  value=" + sort);
	        }
        }
    	return null;
    }

	private String makeDesignersAutocompleteText() throws ConfigurationException {
		DesignerTable designerTable;
		String result;
		
		// This is an obvious candidate for caching but that's not being demonstrated
		designerTable = Model.getInstance().getDesignerTable();
		result = designerTable.findAll().stream().map(d -> d.getName().replace("\"", "\\\"")).sorted().collect(Collectors.joining("\",\n\"","\"", "\""));
		return result;
	}

	private String makeFamiliesAutocompleteText() throws ConfigurationException {
		GameTable gameTable;
		String result;
		
		// This is an obvious candidate for caching but that's not being demonstrated
		gameTable = Model.getInstance().getGameTable();
		result = gameTable.getFamilies().stream().map(d -> d.replace("\"", "\\\"")).sorted().collect(Collectors.joining("\",\n\"","\"", "\""));
		return result;
	}

	@Actions({
	    @Action("/ViewGameList"),
	    @Action("/")
	})
    @Override
    public String execute() throws Exception {
    	GameIdFinderByDesigner gameIdFinderByDesigner;
    	GameListFinderByFamily gameListFinderByFamily;
    	GameListFinderByNameContains gameListFinderByNameContains;
    	GameListSizeFinderByRank gameListSizeFinderByRank;
    	GameItemSorter itemSorter;
    	ListCache<NA,Integer,GameDTO> listCache;
    	HttpSession session;
    	GameCategory preferredCategory;
    	
    	session = getRequest().getSession();
    	
	    preferredCategory = GameCategory.ALL;
    	listCache = GameCompositeCache.getInstance(session).getMasterListCache();
    	if (listCache.getListSizeFinder() instanceof GameListSizeFinderByRank) {
    		gameListSizeFinderByRank = (GameListSizeFinderByRank)listCache.getListSizeFinder();
    		preferredCategory = gameListSizeFinderByRank.getCategory();
    	}
    	itemSorter = (GameItemSorter)listCache.getItemSorter();
	    if (sort != null) {
	    	itemSorter = getItemSorter(preferredCategory);
            listCache.setItemSorter(itemSorter);
            listCache.markReSort();
	    } else {
	    	if (itemSorter instanceof GameItemSorterRank) {
	    		((GameItemSorterRank)listCache.getItemSorter()).setPreferredCategory(preferredCategory);
	    	}
	    }
    	if (refresh != null) {
    		listCache.forceReload();
    	}
	    if (pageNo != null) {
	    	listCache.setPageNo(pageNo);
	    }
	    
	    if (gameNameContainsForm == null) {
	    	if (listCache.getBaseRecordListFinder() instanceof GameListFinderByNameContains) {
	    		gameListFinderByNameContains = (GameListFinderByNameContains)listCache.getBaseRecordListFinder();
	    		gameNameContainsForm = new GameNameContainsForm(gameListFinderByNameContains.getNameContains());
	    	} else {
	    		gameNameContainsForm = new GameNameContainsForm();
	    	}
	    }
	    if (gameRankForm == null) {
	    	if (listCache.getListSizeFinder() instanceof GameListSizeFinderByRank) {
	    		gameListSizeFinderByRank = (GameListSizeFinderByRank)listCache.getListSizeFinder();
		    	gameRankForm = new GameRankForm(gameListSizeFinderByRank.getCategory(), gameListSizeFinderByRank.getRankRangeStart(),
		    			gameListSizeFinderByRank.getRankRangeEnd());
	    	} else {
		    	gameRankForm = new GameRankForm();
	    	}
	    }
	    if (designerNameForm == null) {
	    	if (listCache.getIdListFinder() instanceof GameIdFinderByDesigner) {
	    		gameIdFinderByDesigner = (GameIdFinderByDesigner)listCache.getIdListFinder();
	    		designerNameForm = new DesignerNameForm(gameIdFinderByDesigner.getDesignerName());
	    	} else {
	    		designerNameForm = new DesignerNameForm();
	    	}
	    }
	    if (gameFamilyForm == null) {
	    	if (listCache.getListFinder() instanceof GameListFinderByFamily) {
	    		gameListFinderByFamily = (GameListFinderByFamily)listCache.getListFinder();
	    		gameFamilyForm = new GameFamilyForm(gameListFinderByFamily.getFamily());
	    	} else {
	    		gameFamilyForm = new GameFamilyForm();
	    	}
	    }
	    
	    listDisplay = new GameListDisplay(listCache, preferredCategory);
	    listDisplay.init();
	    
	    designersAutocompleteText = makeDesignersAutocompleteText();
	    familiesAutocompleteText = makeFamiliesAutocompleteText();
	    if (itemSorter != null) {
	        sortOption = itemSorter.getSortColumn().name();
	        sortAscending = itemSorter.getAscending();
	    } else {
	        sortOption = "";
	        sortAscending = true;
	    }
		return SUCCESS;
    }

	public String getRefresh() {
		return refresh;
	}
	public void setRefresh(String refresh) {
		this.refresh = refresh;
	}

	public Integer getPageNo() {
		return pageNo;
	}
	public void setPageNo(Integer pageNo) {
		this.pageNo = pageNo;
	}

	public String getSort() {
		return sort;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}

	public Boolean getAscending() {
		return ascending;
	}
	public void setAscending(Boolean ascending) {
		this.ascending = ascending;
	}

	public GameNameContainsForm getGameNameContainsForm() {
		return gameNameContainsForm;
	}
	public GameRankForm getGameRankForm() {
		return gameRankForm;
	}
	public DesignerNameForm getDesignerNameForm() {
		return designerNameForm;
	}
	public GameFamilyForm getGameFamilyForm() {
		return gameFamilyForm;
	}
	
    public GameListDisplay getListDisplay() {
	    return listDisplay;
    }

	public String getDesignersAutocompleteText() {
		return designersAutocompleteText;
	}
	public String getFamiliesAutocompleteText() {
		return familiesAutocompleteText;
	}
    public String getSortOption() {
        return sortOption;
    }
    public boolean getSortAscending() {
        return sortAscending;
    }
    


}